package com.yylending.las.lending.process.supplyinfo;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.AESEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * P:补充资料
 * Created by fan on 2017/6/16.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class SupplementInfoPresenter extends Presenter<SupplementInfoModel, SupplementInfoPresenter.View> {
	private static final String TAG = SupplementInfoPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<SupplementInfoPresenter> {
		@Override
		public SupplementInfoPresenter create() {
			return new SupplementInfoPresenter();
		}
	}

	private View mView;

	@Override
	public void attach(SupplementInfoPresenter.View view) {
		mView = view;
		mModel = new SupplementInfoModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	protected interface View extends IView {
		void saveSupplyInfoSuccess();
		void saveSupplyInfoFailure(String tips);
	}

	public void saveSupplyInfo(SupplementInfoModel.SupplyInfo params) {
		execute(new AESEntity(params), SupplementInfoModel.FUNCTION_CODE_SAVE_SUPPLY_INFO, true);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void obtainResult(SupplementInfoModel.SaveSupplyInfoResult result) {
		if (BaseResultEntity.isResponseSuccess(result)) {
			mView.saveSupplyInfoSuccess();
		} else {
			mView.saveSupplyInfoFailure(result.getRetMsg());
		}
	}
}
